module facade {

    export class GameFacade extends egret.EventDispatcher {

        private static _instance: GameFacade;

        public static getInstance(): GameFacade {
            if (GameFacade._instance == null) {
                GameFacade._instance = new GameFacade();
            }
            return GameFacade._instance;
        }

        public gamePage: game.GameContainer;

        private sceneCache: Array<egret.DisplayObject> = [];

        private sceneRigister: Array<any> = [];

        private constructor() {
            super();
        }

        public init(): void {

            this.sceneRigister[game.SceneIds.START_SCENE_ID] = scene.StartScene;
            this.sceneRigister[game.SceneIds.GAMEING_SCENE_ID] = scene.NormalGameScene;
            this.sceneRigister[game.SceneIds.OPEN_BENEFITS_SCENE_ID] = scene.StartScene;
            this.sceneRigister[game.SceneIds.OPEN_BOX_SCENE_ID] = scene.StartScene;
            this.sceneRigister[game.SceneIds.SUCCESS_SCENE_ID] = game.GameContainer;
            this.sceneRigister[game.SceneIds.COUNTDOWN_SCENE_ID] = scene.GameRankScene;
            this.sceneRigister[game.SceneIds.GAMEOVER_SCENE_ID] = scene.StartScene;
            this.sceneRigister[game.SceneIds.RANK_SCENE_ID] = scene.GameRankScene;            
            manager.SoundManager.playSound("bgMusic_mp3", false, 0, 0)

            this.addEventListener(events.SceneEvent.SWITCH_SCENE, this.onSwitchScene, this);

            let event: events.SceneEvent = new events.SceneEvent(game.SceneIds.START_SCENE_ID, {});
            this.dispatchEvent(event);
        }

        private onSwitchScene(eve: events.SceneEvent): void {

            console.log('onSwitchSceneonSwitchSceneonSwitchScene')
            // manager.StatisticsManager.sceneCount(1001)

            if(eve.sceneId == 0){
                manager.StatisticsManager.sceneCount(1002)
            }else if(eve.sceneId == 1){
                manager.StatisticsManager.sceneCount(1004)
            }else if(eve.sceneId == 7){
                manager.StatisticsManager.sceneCount(1020)
            }


            manager.GameManager.getInstance().getAdManager().showBannerAd();
            manager.GameManager.getInstance().adManager.showBanner(true);
            if (!this.sceneCache[eve.sceneId]) {
                egret.log("场景id:{" + eve.sceneId + "}不存在,新建场景");
                this.sceneCache[eve.sceneId] = new this.sceneRigister[eve.sceneId];
            }
            let scene: any = this.sceneCache[eve.sceneId];
            if (eve.showType == events.SceneEvent.FULLSCREEN_SHOW_TYPE) {
                this.hideAllScenes();
            }

            egret.log("场景:{" + eve.sceneId + "}");
            manager.GameManager.getInstance().getUIManager().addGameWidget(scene);
            scene.update();
        }

        public resize(): void {

        }

        public playGame(level: number, gameType: number): void {
            this.hideAllScenes();
            manager.GameManager.getInstance().getAdManager().showBannerAd();
            // setTimeout(() => {
                // manager.GameManager.getInstance().adManager.showBanner(true);   
            // }, 1000);
            manager.GameManager.getInstance().hideGameCludButton();

            egret.log("GameFacade.playGame");
            let event: events.PlayGameEvent = new events.PlayGameEvent(level, gameType);
            this.dispatchEvent(event);
        }

        // public passedLevelNormalGame(level: number): void {
        //     egret.log("GameFacade.passedLevelNormalGame");
        //     let event: events.NormalGameLevelPassedEvent = new events.NormalGameLevelPassedEvent(level);
        //     this.dispatchEvent(event);
        // }

        private hideAllScenes(): void {
            let gameLevel = manager.GameManager.getInstance().getUIManager().gameLevel;
            manager.DisplayManager.removeAllChildren(gameLevel);
        }

        private rankPage: scene.GameRankScene;

        public showRank(): void {
            if (this.rankPage == null) {
                this.rankPage = new scene.GameRankScene();
            }
            manager.GameManager.getInstance().getUIManager().addTopWidget(this.rankPage);
        }

        public relive(): void {

        }

        // public showStartPage(): void {
        //     if (!manager.GameManager.getInstance().adManager.isChecking()) {
        //         if (this.gameAdWidget == null) {
        //             this.gameAdWidget = new game.GameAdPage();
        //         }
        //         manager.GameManager.getInstance().getUIManager().addTopWidget(this.gameAdWidget);
        //     }
        //     manager.GameManager.getInstance().showGameClubButton();
        //     this.hideAllPages();
        //     if (this.startPage == null) {
        //         this.startPage = new scene.StartPage();
        //     }
        //     manager.GameManager.getInstance().getUIManager().addGameWidget(this.startPage);
        //     manager.GameManager.getInstance().adManager.showBannerAd();
        //     manager.GameManager.getInstance().adManager.showBanner(true);
        //     this.startPage.show();
        //     this.startPage.addEventListener(scene.StartPage.PLAY_GAME_EVENT, this.showLevelSelectPage, this);
        //     if (manager.GameManager.isProduct) {
        //         let openDataContext = wx.getOpenDataContext();
        //         openDataContext.postMessage({
        //             command: page.PkFriendPage.Pk_Friends,
        //             score: 0,
        //         });
        //     }
        // }
        //
        // public showLevelSelectPage(): void {
        //     this.hideAllPages();
        //     if (this.levelSelectPage == null) {
        //         this.levelSelectPage = new scene.NormalGameLevelSelectScene();
        //     }
        //     this.levelSelectPage.show();
        //     manager.GameManager.getInstance().getUIManager().addGameWidget(this.levelSelectPage);
        //     manager.GameManager.getInstance().getAdManager().showBannerAd();
        // }
        //
        // public showLimitTimeLevelSelectPage(): void {
        //     this.hideAllPages();
        //     if (this.limitTimeSelectPage == null) {
        //         this.limitTimeSelectPage = new scene.LimitTimeLevelSelectScene();
        //     }
        //     this.limitTimeSelectPage.show();
        //     manager.GameManager.getInstance().getUIManager().addGameWidget(this.limitTimeSelectPage);
        //     manager.GameManager.getInstance().getAdManager().showBannerAd();
        // }

        // public showPkPage(isJoin: boolean = false, roomId: string = ""): void {
        //     if (this.pkPage == null) {
        //         this.pkPage = new game.PKMatchScene();
        //     }
        //     manager.GameManager.getInstance().getUIManager().addGameWidget(this.pkPage);
        //     this.pkPage.setInfo(isJoin, roomId);
        //     this.pkPage.start();
        // }
    }

}